package com.wxmit.framework.web.intercepter;

import com.wxmit.base.util.ThreadMdcUtil;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author huangxiong
 * @date 2024/04/02 09:20
 */
public class LogTraceInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //log.info("进入 LogInterceptor");
        String traceId = request.getHeader(ThreadMdcUtil.TRACE_ID);
        ThreadMdcUtil.setTraceIdIfNull(traceId);

        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        ThreadMdcUtil.removeTraceId();
    }

}
